import type { Request, Response } from 'express';

// 模拟用户数据
const mockUsers = [
  {
    id: '1',
    name: '张三',
    email: 'zhangsan@example.com',
    status: 'active',
    role: 'admin',
    createTime: '2024-01-15 10:30:00',
    description: '系统管理员，负责整体系统维护',
  },
  {
    id: '2',
    name: '李四',
    email: 'lisi@example.com',
    status: 'active',
    role: 'user',
    createTime: '2024-01-16 14:20:00',
    description: '普通用户，日常业务操作',
  },
  {
    id: '3',
    name: '王五',
    email: 'wangwu@example.com',
    status: 'inactive',
    role: 'guest',
    createTime: '2024-01-17 09:15:00',
    description: '访客用户，仅可查看信息',
  },
  {
    id: '4',
    name: '赵六',
    email: 'zhaoliu@example.com',
    status: 'active',
    role: 'user',
    createTime: '2024-01-18 16:45:00',
    description: '测试用户账号',
  },
  {
    id: '5',
    name: '孙七',
    email: 'sunqi@example.com',
    status: 'inactive',
    role: 'guest',
    createTime: '2024-01-19 11:20:00',
    description: '临时访客账号',
  },
  {
    id: '6',
    name: '周八',
    email: 'zhouba@example.com',
    status: 'active',
    role: 'user',
    createTime: '2024-01-20 08:30:00',
    description: '业务部门用户，负责客户管理',
  },
  {
    id: '7',
    name: '吴九',
    email: 'wujiu@example.com',
    status: 'active',
    role: 'admin',
    createTime: '2024-01-21 15:45:00',
    description: '技术管理员，负责系统开发',
  },
  {
    id: '8',
    name: '郑十',
    email: 'zhengshi@example.com',
    status: 'inactive',
    role: 'user',
    createTime: '2024-01-22 12:10:00',
    description: '财务部门用户',
  },
  {
    id: '9',
    name: '钱十一',
    email: 'qianshiyi@example.com',
    status: 'active',
    role: 'user',
    createTime: '2024-01-23 09:25:00',
    description: '人事部门用户，负责员工管理',
  },
  {
    id: '10',
    name: '孙十二',
    email: 'sunshier@example.com',
    status: 'active',
    role: 'guest',
    createTime: '2024-01-24 16:50:00',
    description: '外部合作伙伴',
  },
  {
    id: '11',
    name: '李十三',
    email: 'lishisan@example.com',
    status: 'inactive',
    role: 'user',
    createTime: '2024-01-25 11:35:00',
    description: '销售部门用户',
  },
  {
    id: '12',
    name: '王十四',
    email: 'wangshisi@example.com',
    status: 'active',
    role: 'admin',
    createTime: '2024-01-26 14:20:00',
    description: '运营管理员，负责系统运营',
  },
  {
    id: '13',
    name: '赵十五',
    email: 'zhaoshiwu@example.com',
    status: 'active',
    role: 'user',
    createTime: '2024-01-27 10:15:00',
    description: '客服部门用户',
  },
  {
    id: '14',
    name: '陈十六',
    email: 'chenshiliu@example.com',
    status: 'inactive',
    role: 'guest',
    createTime: '2024-01-28 13:40:00',
    description: '临时访问用户',
  },
  {
    id: '15',
    name: '刘十七',
    email: 'liushiqi@example.com',
    status: 'active',
    role: 'user',
    createTime: '2024-01-29 08:55:00',
    description: '市场部门用户',
  },
  {
    id: '16',
    name: '黄十八',
    email: 'huangshiba@example.com',
    status: 'active',
    role: 'admin',
    createTime: '2024-01-30 17:30:00',
    description: '安全管理员，负责系统安全',
  },
  {
    id: '17',
    name: '林十九',
    email: 'linshijiu@example.com',
    status: 'inactive',
    role: 'user',
    createTime: '2024-02-01 12:05:00',
    description: '研发部门用户',
  },
  {
    id: '18',
    name: '何二十',
    email: 'haershi@example.com',
    status: 'active',
    role: 'user',
    createTime: '2024-02-02 15:20:00',
    description: '产品部门用户',
  },
  {
    id: '19',
    name: '高二十一',
    email: 'gaoershiyi@example.com',
    status: 'active',
    role: 'guest',
    createTime: '2024-02-03 09:45:00',
    description: '外部咨询顾问',
  },
  {
    id: '20',
    name: '马二十二',
    email: 'maershier@example.com',
    status: 'inactive',
    role: 'user',
    createTime: '2024-02-04 16:10:00',
    description: '质量部门用户',
  },
];

export default {
  // 获取用户列表
  'GET /api/anyics-front/demo/users': (req: Request, res: Response) => {
    const { current = 1, pageSize = 10, name, email, status, role } = req.query;

    // 过滤数据
    let filteredUsers = [...mockUsers];

    if (name) {
      filteredUsers = filteredUsers.filter((user) => user.name.toLowerCase().includes((name as string).toLowerCase()));
    }

    if (email) {
      filteredUsers = filteredUsers.filter((user) =>
        user.email.toLowerCase().includes((email as string).toLowerCase()),
      );
    }

    if (status) {
      filteredUsers = filteredUsers.filter((user) => user.status === status);
    }

    if (role) {
      filteredUsers = filteredUsers.filter((user) => user.role === role);
    }

    // 分页
    const start = (Number(current) - 1) * Number(pageSize);
    const end = start + Number(pageSize);
    const paginatedUsers = filteredUsers.slice(start, end);

    // 模拟延迟
    setTimeout(() => {
      res.json({
        data: paginatedUsers,
        total: filteredUsers.length,
        success: true,
        current: Number(current),
        pageSize: Number(pageSize),
      });
    }, 300);
  },

  // 获取用户列表 (POST 方式 - 用于复杂查询)
  'POST /api/anyics-front/demo/users': (req: Request, res: Response) => {
    const { current = 1, pageSize = 10, name, email, status, role } = req.body;

    // 过滤数据
    let filteredUsers = [...mockUsers];

    if (name) {
      filteredUsers = filteredUsers.filter((user) => user.name.toLowerCase().includes((name as string).toLowerCase()));
    }

    if (email) {
      filteredUsers = filteredUsers.filter((user) =>
        user.email.toLowerCase().includes((email as string).toLowerCase()),
      );
    }

    if (status && status !== 'all') {
      filteredUsers = filteredUsers.filter((user) => user.status === status);
    }

    if (role) {
      filteredUsers = filteredUsers.filter((user) => user.role === role);
    }

    // 分页
    const start = (Number(current) - 1) * Number(pageSize);
    const end = start + Number(pageSize);
    const paginatedUsers = filteredUsers.slice(start, end);

    // 模拟延迟
    setTimeout(() => {
      res.json({
        data: paginatedUsers,
        total: filteredUsers.length,
        success: true,
        current: Number(current),
        pageSize: Number(pageSize),
      });
    }, 300);
  },

  // 创建用户
  'POST /api/demo/users/create': (req: Request, res: Response) => {
    const newUser = {
      id: Math.random().toString(36).substr(2, 9),
      ...req.body,
      createTime: new Date().toLocaleString('zh-CN'),
    };

    mockUsers.unshift(newUser);

    setTimeout(() => {
      res.json({
        data: newUser,
        success: true,
        message: '创建成功',
      });
    }, 500);
  },

  // 更新用户 (PUT)
  'PUT /api/demo/users/:id': (req: Request, res: Response) => {
    const { id } = req.params;
    const userIndex = mockUsers.findIndex((user) => user.id === id);

    if (userIndex === -1) {
      return res.status(404).json({
        success: false,
        message: '用户不存在',
      });
    }

    mockUsers[userIndex] = {
      ...mockUsers[userIndex],
      ...req.body,
    };

    setTimeout(() => {
      res.json({
        data: mockUsers[userIndex],
        success: true,
        message: '更新成功',
      });
    }, 500);
  },

  // 删除用户 (DELETE)
  'DELETE /api/demo/users/:id': (req: Request, res: Response) => {
    const { id } = req.params;
    const userIndex = mockUsers.findIndex((user) => user.id === id);

    if (userIndex === -1) {
      return res.status(404).json({
        success: false,
        message: '用户不存在',
      });
    }

    mockUsers.splice(userIndex, 1);

    setTimeout(() => {
      res.json({
        success: true,
        message: '删除成功',
      });
    }, 500);
  },

  // 获取用户详情 (GET)
  'GET /api/demo/users/:id': (req: Request, res: Response) => {
    const { id } = req.params;
    const user = mockUsers.find((user) => user.id === id);

    if (!user) {
      return res.status(404).json({
        success: false,
        message: '用户不存在',
      });
    }

    setTimeout(() => {
      res.json({
        data: user,
        success: true,
      });
    }, 300);
  },

  // 批量删除用户
  'DELETE /api/demo/users/batch': (req: Request, res: Response) => {
    const { ids } = req.body;

    if (!Array.isArray(ids)) {
      return res.status(400).json({
        success: false,
        message: '参数错误',
      });
    }

    ids.forEach((id) => {
      const userIndex = mockUsers.findIndex((user) => user.id === id);
      if (userIndex !== -1) {
        mockUsers.splice(userIndex, 1);
      }
    });

    setTimeout(() => {
      res.json({
        success: true,
        message: `成功删除 ${ids.length} 条记录`,
      });
    }, 800);
  },
};
